import { defineStore } from "pinia";
import { useSearchState } from '../interface'
import { reqSearcher } from '@/api/search/index'
export const useSearchStore = defineStore('search', {
    state(): useSearchState {
        return {
            isLoading: false,
            searchParams: {
                keyword: '',
                page: 1,
                size: 12,
                sortType: 0,
                descSorted: true,
                categoryId: 0,
                matchType: 0,
                floorPrice: -1,
                upperPrice: -1,
                stillSearch: false,
                searchWordSource: 0,

            },
            searcherCategoryHitCountList: [],
            searcherResult: {
                pagination: {
                    page: 0,
                    size: 0,
                    totalPage: 0,
                    total: 0,
                    lastPage: true
                },
                result: []
            },

        }
    },
    actions: {
        async search() {
            this.$state.isLoading = true;
            let res = await reqSearcher(this.searchParams)
            this.$patch(state => {
                state.searcherCategoryHitCountList = res.directly?.searcherCategoryHitCountList
                state.searcherResult = res.directly?.searcherResult
                state.searchParams = { ...state.searchParams, ...res.searchParams }
                state.isLoading = false
            })

        }
    },
    getters: {
        goodList(state) {
            return state.searcherResult?.result
        },
        pagination(state) {
            return state.searcherResult?.pagination
        },
        category(state) {
            return state.searchParams.parentCategoryId
                ? {
                    categoryId: state.searchParams.parentCategoryId,
                    categoryName: state.searchParams.parentCategoryName
                }
                : {
                    categoryId: state.searchParams.categoryId,
                    categoryName: state.searchParams.categoryName
                }
        },
        subCategory(state) {
            return state.searchParams.parentCategoryId
                ? {
                    subCategoryId: state.searchParams.categoryId,
                    subCategoryName: state.searchParams.categoryName
                } : {
                    subCategoryId: 0,
                    subCategoryName: undefined
                }
        }
    }
})